<?php
include_once 'root.php';
Class User extends  Root {
	public function __construct() {
		parent::__construct();
		
		
		
	}

	public function index() {
		$this -> login();
	}

	public function login() {
			
			
		if ($this -> getLoginStatus()) {
			redirect(base_url() . "forum/");
			exit();
		} else {
			$this -> addJS(array());
			$this -> load -> view("login", $this -> data);
		}

	}

	public function logout() {
		$this -> session -> sess_destroy();
		redirect(base_url() . "user/login/");

	}

	public function loginAction() {
		$data = $this -> Post2Array($_POST);
		$this -> load -> model("usermodel");

		$data = $this -> usermodel -> loginData($data);
		if ($data != false) {
			$myAuthen = $this -> usermodel -> getMyAuthen($data -> UserID);
			if (!empty($myAuthen) || strtolower($data -> ROLE)=="admin") {
				$this -> session -> set_userdata("AUTHEN", $myAuthen);
				$this -> setSaleID($data -> SALES_NO);
				$this -> setSaleName($data -> SALES_NAME);
				$this -> setUserLevel($data -> ROLE);
				$this -> setLoginStatus();
				$this -> setUserID($data -> UserID);
				$this -> set_res("true", "");
			} else {
				$this -> set_res("false", "user นี้ยังไม่ได้ทำการเปิดสิทธิการใช้งาน");
			}

		} else {
			$this -> set_res("false", "ไม่สามารถเข้าสู่ระบบได้");
		}
	}

}
?>